class Solution {
public:
    int lengthOfLastWord(string s) {
        int ans = 0;
        int i = s.length() - 1;
        while(s[i] == ' ' && i >= 0)i--;
        while(s[i] != ' '){
            ans++;
            i--;
            if(i < 0)break;
        }
        return ans;
    }
};